package com.inossem.allotin.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.allotin.bean.offLine.OffLineInfoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 离线工具
 * 模块名称和单据抬头 保存在ACache文件夹
 * 单据信息 保存在document_info文件夹
 *
 * @author 詹建宇
 * @time on 2018/12/19 17:44
 * @email jianyu.zhan@inossem.com
 */
public class OffLineUtils {

    //=====================================--module--===============================================
    public final static String SaleOutStorage = "SaleOutStorage";//销售出库
    public final static String PickOutStorage = "PickOutStorage";//领料出库
    public final static String PurchaseReturnMats = "PurchaseReturnMats";//采购退货
    public final static String DownFrame = "DownFrame";//下架
    public final static String PURCHASEINPUT = "purchaseInput";//采购入库
    public final static String TRANSPORT_INPUT = "transport_input";//调拨接收
    public final static String PRODUCTIONINPUT = "productionInput";//生产入库
    public final static String INSPECTINPUT = "inspectInput";//验收入库
    public final static String PUTAWAYMANAGER = "putawayManager";//上架
    //=====================================--module--===============================================

    //离线相关的Bean类 里面存模块列表 和模块下面的离线单据抬头列表(默认ACache文件夹)
    private static final String KEY_OFFLINE = "key_offline";

    //保存单据信息列表 document_info
    private static final String DOCUMENT_INFO = "document_info";

    /**
     * 增加一条单据
     *
     * @param module           模块名称
     * @param documentInfoBean 单据信息 (key为主键 传时间戳 保证key唯一)
     * @param object           单据内容
     */
    public static void addDocument(String module, OffLineInfoBean.ModuleBean.DocumentInfoBean documentInfoBean, Object object) {
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);

        if (null == offLineInfoBean) {
            offLineInfoBean = new OffLineInfoBean();
        }

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList) {
            moduleList = new ArrayList<>();
            offLineInfoBean.setModuleList(moduleList);
        }

        Integer whichModule = null;
        for (int i = 0; i < moduleList.size(); i++) {
            if (module.equals(moduleList.get(i).getModuleName())) {
                whichModule = i;
                break;
            }
        }

        OffLineInfoBean.ModuleBean moduleBean;
        if (null == whichModule) {
            moduleBean = new OffLineInfoBean.ModuleBean();
            moduleBean.setModuleName(module);
            moduleList.add(moduleBean);
        } else {
            moduleBean = moduleList.get(whichModule);
        }

        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentInfoList = moduleBean.getDocumentInfoList();
        if (null == documentInfoList) {
            documentInfoList = new ArrayList<>();
            moduleBean.setDocumentInfoList(documentInfoList);
        }

        Integer whichDocument = null;
        for (int i = 0; i < documentInfoList.size(); i++) {
            if (documentInfoBean.getCode().equals(documentInfoList.get(i).getCode())) {
                whichDocument = i;
                break;
            }
        }

        ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);

        if (null != whichDocument) {
            documentInfoList.remove((int) whichDocument);
            documentInfoList.add(0, documentInfoBean);
            aCache_DocumentInfo.remove(module + "-" + documentInfoBean.getCode());
        } else {
            documentInfoList.add(0, documentInfoBean);
        }
        aCache_DocumentInfo.put(module + "-" + documentInfoBean.getCode(), new Gson().toJson(object), ACacheUtils.getSaveTime());

        aCache_ACache.put(KEY_OFFLINE, offLineInfoBean);
    }

    /**
     * 删除一条单据 根据key
     *
     * @param module 模块名称
     * @param key    单据主键
     */
    public static void deleteDocument(String module, int key) {
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);

        if (null == offLineInfoBean) return;

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList || moduleList.isEmpty()) return;

        OffLineInfoBean.ModuleBean moduleBean = null;
        for (OffLineInfoBean.ModuleBean bean : moduleList) {
            if (module.equals(bean.getModuleName())) {
                moduleBean = bean;
                break;
            }
        }
        if (null == moduleBean) return;

        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentInfoList = moduleBean.getDocumentInfoList();
        if (null == documentInfoList || documentInfoList.isEmpty()) return;

        Integer whichDocument = null;
        for (int i = 0; i < documentInfoList.size(); i++) {
            if (key == (documentInfoList.get(i).getKey())) {
                whichDocument = i;
                break;
            }
        }

        if (null == whichDocument) return;

        ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);
        aCache_DocumentInfo.remove(module + "-" + documentInfoList.get(whichDocument).getCode());

        documentInfoList.remove((int) whichDocument);
        aCache_ACache.put(KEY_OFFLINE, offLineInfoBean);
    }

    /**
     * 删除一条单据 根据code
     *
     * @param module 模块名称
     * @param code   单号
     */
    public static void deleteDocument(String module, String code) {
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);

        if (null == offLineInfoBean) return;

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList || moduleList.isEmpty()) return;

        OffLineInfoBean.ModuleBean moduleBean = null;
        for (OffLineInfoBean.ModuleBean bean : moduleList) {
            if (module.equals(bean.getModuleName())) {
                moduleBean = bean;
                break;
            }
        }
        if (null == moduleBean) return;

        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentInfoList = moduleBean.getDocumentInfoList();
        if (null == documentInfoList || documentInfoList.isEmpty()) return;

        Integer whichDocument = null;
        for (int i = 0; i < documentInfoList.size(); i++) {
            if (code.equals(documentInfoList.get(i).getCode())) {
                whichDocument = i;
                break;
            }
        }

        if (null == whichDocument) return;

        ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);
        aCache_DocumentInfo.remove(module + "-" + documentInfoList.get(whichDocument).getCode());

        documentInfoList.remove((int) whichDocument);
        aCache_ACache.put(KEY_OFFLINE, offLineInfoBean);
    }

    /**
     * 查询该模块下所有单据
     *
     * @param module 模块名称
     * @return 单据列表
     */
    public static List<OffLineInfoBean.ModuleBean.DocumentInfoBean> selectDocumentList(String module) {
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);

        if (null == offLineInfoBean) return new ArrayList<>();

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList || moduleList.isEmpty()) return new ArrayList<>();

        for (OffLineInfoBean.ModuleBean moduleBean : moduleList) {
            if (module.equals(moduleBean.getModuleName())) {
                List<OffLineInfoBean.ModuleBean.DocumentInfoBean> list = moduleBean.getDocumentInfoList();

                for (int i = list.size() - 1; i >= 0; i--) {
                    OffLineInfoBean.ModuleBean.DocumentInfoBean infoBean = list.get(i);
                    int infoTime = infoBean.getKey();//单据保存时间点
                    int saveTime = ACacheUtils.getSaveTime();//离线时间段
                    int nowTime = TimeUtils.getTimeSeal();//当前时间

                    if (nowTime - infoTime > saveTime) {
                        list.remove(i);
                        deleteDocument(module, infoBean.getKey());
                    }
                }

                return list;
            }
        }

        return new ArrayList<>();
    }

    /**
     * 查询该模块下 某个单据的信息
     *
     * @param module 模块名称
     * @param key    单据主键
     * @return 单据的信息
     */
    public static String selectDocumentInfo(String module, int key) {
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);

        if (null == offLineInfoBean) return null;

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList || moduleList.isEmpty()) return null;

        OffLineInfoBean.ModuleBean moduleBean = null;
        for (OffLineInfoBean.ModuleBean bean : moduleList) {
            if (module.equals(bean.getModuleName())) {
                moduleBean = bean;
                break;
            }
        }
        if (null == moduleBean) return null;

        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentInfoList = moduleBean.getDocumentInfoList();
        if (null == documentInfoList || documentInfoList.isEmpty()) return null;

        for (int i = 0; i < documentInfoList.size(); i++) {
            if (key == (documentInfoList.get(i).getKey())) {
                ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);
                return aCache_DocumentInfo.getAsString(module + "-" + documentInfoList.get(i).getCode());
            }
        }

        return null;
    }

    /**
     * 查询该模块下 某个单据的信息
     *
     * @param module 模块名称
     * @param code   单据Code
     * @return 单据的信息
     */
    public static String selectDocumentInfo(String module, String code) {
        if (TextUtils.isEmpty(code)) return null;
        ACache aCache_ACache = ACache.get();
        OffLineInfoBean offLineInfoBean = (OffLineInfoBean) aCache_ACache.getAsObject(KEY_OFFLINE);
        if (null == offLineInfoBean) return null;

        List<OffLineInfoBean.ModuleBean> moduleList = offLineInfoBean.getModuleList();
        if (null == moduleList || moduleList.isEmpty()) return null;

        OffLineInfoBean.ModuleBean moduleBean = null;
        for (OffLineInfoBean.ModuleBean bean : moduleList) {
            if (module.equals(bean.getModuleName())) {
                moduleBean = bean;
                break;
            }
        }
        if (null == moduleBean) return null;

        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentInfoList = moduleBean.getDocumentInfoList();
        if (null == documentInfoList || documentInfoList.isEmpty()) return null;

        for (int i = 0; i < documentInfoList.size(); i++) {
            if (!TextUtils.isEmpty(documentInfoList.get(i).getCode()) && code.equals(documentInfoList.get(i).getCode())) {
                ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);
                return aCache_DocumentInfo.getAsString(module + "-" + documentInfoList.get(i).getCode());
            }
        }

        return null;
    }

    /**
     * 获取该模块下草稿状态的单据(有Code)
     *
     * @param module 模块名称
     */
    public static List<OffLineInfoBean.ModuleBean.DocumentInfoBean> selectDocumentListWithCode(String module) {
        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> list = selectDocumentList(module);
        for (int i = list.size() - 1; i >= 0; i--) {
            if (TextUtils.isEmpty(list.get(i).getCode())) {
                list.remove(i);
            }
        }
        return list;
    }

    /**
     * 获取该模块下创建状态的单据(无Code)
     *
     * @param module 模块名称
     */
    public static List<OffLineInfoBean.ModuleBean.DocumentInfoBean> selectDocumentListWithoutCode(String module) {
        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> list = selectDocumentList(module);
        for (int i = list.size() - 1; i >= 0; i--) {
            if (!TextUtils.isEmpty(list.get(i).getCode())) {
                list.remove(i);
            }
        }
        return list;
    }

    /**
     * 获取该模块下的前置单据
     *
     * @param module 模块名称
     * @return 前置单据
     */
    public static List<OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean> selectPreDocumentList(String module) {
        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> list = selectDocumentListWithoutCode(module);
        List<OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean> preDocumentInfoList = new ArrayList<>();
        for (OffLineInfoBean.ModuleBean.DocumentInfoBean documentInfoBean : list) {
            preDocumentInfoList.addAll(documentInfoBean.getPreDocumentInfoList());
        }
        return preDocumentInfoList;
    }

    /**
     * 根据前置单据列表 获取已创建状态的单据(创建状态的单据,没有单号)
     *
     * @param module  模块名称
     * @param preList 前置单据列表(PreDocumentInfoBean类 传preType、preCode就可以)
     * @return 已创建状态的单据
     */
    public static OffLineInfoBean.ModuleBean.DocumentInfoBean selectDocumentInfoByPres(String module
            , List<OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean> preList) {
        List<OffLineInfoBean.ModuleBean.DocumentInfoBean> documentList = selectDocumentListWithoutCode(module);

        //遍历离线单据列表
        for (OffLineInfoBean.ModuleBean.DocumentInfoBean documentInfoBean : documentList) {
            //离线单据下的前置单据列表
            List<OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean> preDocumentInfoList = documentInfoBean.getPreDocumentInfoList();
            //前置单据数量一致 才进行下一步判断
            if (preList.size() == preDocumentInfoList.size()) {

                boolean isSamePres = true;//前置单据列表 是否相同
                for (OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean pre1 : preDocumentInfoList) {
                    boolean isSamePre = false;//前置单据 是否相同
                    for (OffLineInfoBean.ModuleBean.DocumentInfoBean.PreDocumentInfoBean pre2 : preList) {
                        if (pre1.getPreType() == pre2.getPreType() && pre1.getPreCode().equals(pre2.getPreCode())) {
                            isSamePre = true;
                            break;
                        }
                    }
                    if (!isSamePre) {
                        isSamePres = false;
                        break;
                    }
                }

                if (isSamePres) {
                    return documentInfoBean;
                }
            }
        }

        return null;
    }

    /**
     * 清空离线单据信息
     */
    public static void removeAllData() {
        ACache aCache_DocumentInfo = ACache.get(DOCUMENT_INFO);
        aCache_DocumentInfo.clear();

        ACache aCache_ACache = ACache.get();
        aCache_ACache.remove(KEY_OFFLINE);
    }

}
